import datetime

from django.shortcuts import render
from django.http import HttpResponse
from django.http.response import JsonResponse
from django.views.generic import TemplateView
from django.views.generic.edit import FormView

from .models import CertificationInfo
from .forms import CertificationGenerationForm

# Create your views here.

class AjaxableResponseMixin:
    """
    Mixin to add AJAX support to a form.
    Must be used with an object-based FormView.
    """
    def form_invalid(self, form):
        response = super().form_invalid(form)
        if self.request.is_ajax():
            return JsonResponse(form.errors, status=400)
        else:
            return response

    def form_valid(self, form):
        response = super().form_valid(form)
        cert_file = self.request.FILES["cert_file"]
        print("REQUEST", self.request)
        print(cert_file)
        if self.request.is_ajax():
            data = {
                'pk': 1
            }
            return JsonResponse(data)
        else:
            return response


class CertificationGenerateView(AjaxableResponseMixin, FormView):
    template_name = "certifications/certifications.html"
    form_class = CertificationGenerationForm
    success_url = ""
    code_generated = False
    generated_code = ""

    # def get(self, request, *args, **kwargs):
    #     pass

    # def post(self, request, *args, **kwargs):
    #     pass